/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.lehe.common.Contants;
import com.lehe.common.exceptions.LHException;
import com.lehe.common.exceptions.LHServiceException;
import com.lehe.common.utils.StringUtil;
import com.lehe.dao.UserInfoDao;
import com.lehe.entry.UserInfoDto;
import com.lehe.entry.wx.message.RespUserOpenIdMessage;
import com.lehe.entry.wx.message.RespUserUnionIdMessage;
import com.lehe.service.IWeChatExecuteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户账户操作
 *
 * @author 王丹
 * @since 2017-02-26
 */
@Transactional
@Service
public class UserInfoService extends AbstractService {

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    @Qualifier("queryUserOpenIdExecuteService")
    private IWeChatExecuteService queryUserOpenIdExecuteService;
    @Autowired
    @Qualifier("queryUserUnionidIdExecuteService")
    private IWeChatExecuteService queryUserUnionidIdExecuteService;

    /**
     * 根据微信返回的code调用微信接口返回unionId
     * @param wxCode
     * @return
     * @throws LHException
     */
    public String invokeWxForUnionId(String wxCode) throws LHException {
        String wxUnionId = "";
        try {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", Contants.APP_ID);
        map.put("secret", Contants.APP_SECRET);
        map.put("grant_type", Contants.GRANT_TYPE_QUERY_OPENID);
        map.put("code", wxCode);

        RespUserOpenIdMessage respUserOpenIdMessage = (RespUserOpenIdMessage)queryUserOpenIdExecuteService.execute(map);
        if(StringUtil.isBlank(respUserOpenIdMessage.getOpenid())){
            throw new LHException("请在微信公众号中打开");
        }
        map.clear();
        map.put("access_token", respUserOpenIdMessage.getAccessToken());
        map.put("openid", respUserOpenIdMessage.getOpenid());
        map.put("lang", "zh_CN");
        //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        RespUserUnionIdMessage respUserUnionIdMessage = (RespUserUnionIdMessage)queryUserUnionidIdExecuteService.execute(map);
        wxUnionId = respUserUnionIdMessage.getUnionid();
        } catch (LHServiceException e) {
            logger.error("获取wxUnionId失败:", e);
        }
        if(StringUtil.isBlank(wxUnionId)){
            throw new LHException("服务忙，请稍后重试");
        }
        return wxUnionId;
    }

    /**
     * 根据用户ID或者微信UnionId查找用户信息
     * @param userId
     * @param wxUnionId
     * @return
     */
    public UserInfoDto queryUserInfo(Long userId, String wxUnionId){
        UserInfoDto userInfoDto = new UserInfoDto();
        userInfoDto.setUserId(userId);
        userInfoDto.setWxUnionId(wxUnionId);
        return userInfoDao.queryUserInfo(userInfoDto);
    }

    /**
     * 授权代理更新用户账户信息
     * @param userInfoDto
     * @return
     */
    public int updateAccountByUserId(UserInfoDto userInfoDto){
        return userInfoDao.updateAccountByUserId(userInfoDto);
    }

	public UserInfoDto queryUserInfoByAgentId(Long agentId) {
		return userInfoDao.queryUserInfoByAgentId(agentId);
	}
}
